﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ACLCTaguigEnrollmentSystem
{
    public partial class frm_Login : Form
    {
        public frm_Login()
        {
            InitializeComponent();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            if ((txt_Username.Text == null && txt_Password.Text == null) || (txt_Username.Text == "" && txt_Password.Text == ""))
            {
                MessageBox.Show("Please Enter the Username and Password");
            }
            else if (txt_Username.Text == null || txt_Username.Text == "")
            {
                MessageBox.Show("Please Enter the Username");
            }
            else if (txt_Password.Text == null || txt_Password.Text == "")
            {
                MessageBox.Show("Please Enter the Password");
            }
            else
            {
                try
                {
                    SqlConnection conn = new SqlConnection(Classes.ConnectionString.connString);
                    SqlCommand cmd = new SqlCommand("SELECT e.EmployeeNo, a.Password, ul.UserLevelName FROM Account a LEFT JOIN Employees e on a.EmployeeID = e.EmployeeID LEFT JOIN UserLevels ul on a.UserLevelID = ul.UserlevelID WHERE e.EmployeeNo = " + txt_Username.Text + "and a.Password = " + txt_Password.Text + "", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();

                    da.Fill(dt);
                    conn.Open();
                    if (dt.Rows[0].ItemArray[0].ToString() == txt_Username.Text && dt.Rows[0].ItemArray[1].ToString() == txt_Password.Text)
                    {
                        frm_MainForm mf = new frm_MainForm();
                        mf.Show();
                        this.Hide();
                        MessageBox.Show("You successfully Login");
                    }
                    else
                    {
                        MessageBox.Show("Incorrect Username or Password");
                    }
                    conn.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Incorrect Username or Password");
                    //conn.Close(); 
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
